//
//  HttpUtils.m
//  BellManage
//
//  Created by Bestsch on 16/3/29.
//  Copyright © 2016年 wsl. All rights reserved.
//

#import "HttpUtils.h"

@implementation HttpUtils

/**
 *  gb2312的get方法
 *
 *  @param urlstr 地址
 *
 *  @return 服务器返回json字符串
 */
+(NSString *)HttpGetGB:(NSString *)urlstr{
    NSURL *url = [NSURL URLWithString:urlstr];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSHTTPURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error:nil];
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        if ([response statusCode]!=200) {
            return nil;
        }
    }
    NSStringEncoding enc1 = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    NSString *resultStr = [[NSString alloc] initWithData: responseData encoding:enc1];
    
    resultStr = [resultStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    resultStr = [resultStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return resultStr;
}
/**
 *  utf8的get方法
 *
 *  @param urlstr 地址
 *
 *  @return 服务器返回的json字符串
 */
+(NSString *)HttpGetUTF:(NSString *)urlstr{
    NSURL *url = [NSURL URLWithString:urlstr];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSHTTPURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error:nil];
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        if ([response statusCode]!=200) {
            return nil;
        }
    }
    NSString *resultStr = [[NSString alloc] initWithData: responseData encoding:NSUTF8StringEncoding];
    
    resultStr = [resultStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    resultStr = [resultStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return resultStr;
}

/**
 *  gb2312的post方法
 *
 *  @param urlstr     地址
 *  @param requestXml 传递的xml字符串
 *
 *  @return 服务器返回的json字符串
 */
+(NSString *)HttpPostGB:(NSString *)urlstr with:(NSString *)requestXml{
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlstr]];
    [request setHTTPMethod:@"POST"];
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    //    NSLog(@"提交数据：%@",requestXml);
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"%@",requestXml]dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:postBody];
    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSStringEncoding enc1 = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    NSString *resultStr = [[NSString alloc] initWithData: responseData encoding:enc1];
    return resultStr;
}

/**
 *  utf8的post方法
 *
 *  @param urlstr     地址
 *  @param requestXml XML字符串
 *
 *  @return 服务器返回的json字符串
 */
+(NSString *)HttpPostUTF:(NSString *)urlstr with:(NSString *)requestXml{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlstr]];
    [request setHTTPMethod:@"POST"];
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
//    NSLog(@"提交数据：%@",requestXml);
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"%@",requestXml]dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:postBody];
    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *resultStr = [[NSString alloc] initWithData: responseData encoding:NSUTF8StringEncoding];
    return resultStr;
}

@end
